#pragma once

template<typename T>
class safe_wrapper
{
public:
	safe_wrapper(void):_ptr(NULL){}
	safe_wrapper(T* ptr){ attach(ptr); }
	safe_wrapper(const safe_wrapper& other){ *this = other; }
	virtual ~safe_wrapper()
	{
		if(_ptr){
			free();
		}
	}

	virtual void free(void) = 0;

	void attach(T* ptr){ _ptr = ptr; }
	T* detach(void)
	{
		T* temp = _certcontext;
		_ptr = NULL;
		return temp;
	}

	void operator = (const safe_wrapper& other){ _ptr = other._ptr; }
	void operator = (T* ptr){ attach(ptr); }

	operator T*(){ return _ptr; }
	operator T&(){ return *_ptr;}

protected:
	T* _ptr;
};